package com.pureut.storage.controller;

import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.storage.dto.WipCompletionDto;
import com.pureut.storage.export.WipCompletionExport;
import com.pureut.storage.service.WipCompletionService;
import com.pureut.storage.util.ExcelUtil;
import com.pureut.storage.vo.PickingRestDbVo;
import com.pureut.storage.vo.UpdateStatusByQualityVo;
import com.pureut.storage.vo.WipCompletionVo;
import com.pureut.storage.vo.WipOtherAddVo;
import com.pureut.system.api.vo.ReturnStatusVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/***
 * 完工入库控制层
 * Author:C
 * Date:2022/12/30 11:56
 * @DESC
 */
@RestController
@RequestMapping("/wip-completion")
public class WipCompletionController extends BaseController {


    @Resource
    WipCompletionService wipCompletionService;


    /**
     * 获取完工入库分页列表
     */
    @RequiresPermissions("warehouse:warehousing:finished:list")
    @GetMapping("/list")
    public TableDataInfo getList(WipCompletionVo wipCompletionVo) {
        startPage();
        //查询委外入库列表
        List<WipCompletionDto> list = wipCompletionService.getReceiptList(wipCompletionVo);
        return getDataTable(list);
    }

    /**
     * 新增完工入库
     */
    @RequiresPermissions("warehouse:warehousing:finished:add")
    @Log(title = "新增完工入库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult wipAdd(@Validated @RequestBody WipCompletionVo wipCompletionVo) throws Exception {
        return toAjax(wipCompletionService.addWipCompletion(wipCompletionVo));
    }

    /**
     * 新增完工入库（其他模块调用）
     */
    @PostMapping("/wipOtherAdd")
    public AjaxResult wipOtherAdd(@Validated @RequestBody WipOtherAddVo wipOtherAddVo) throws Exception {
        return toAjax(wipCompletionService.wipOtherAdd(wipOtherAddVo));
    }

    /**
     * 关结完工入库
     */
    @RequiresPermissions("warehouse:warehousing:finished:closing")
    @Log(title = "关结完工入库", businessType = BusinessType.UPDATE)
    @PostMapping("/close")
    public AjaxResult close(@RequestBody WipCompletionVo wipCompletionVo) {
        return toAjax(wipCompletionService.close(wipCompletionVo));
    }

    /**
     * 查看
     */
    @RequiresPermissions("warehouse:warehousing:finished:query")
    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id) {
        return AjaxResult.success(wipCompletionService.getView(id));
    }

    /**
     * 修改完工入库
     */
    @RequiresPermissions("warehouse:warehousing:finished:edit")
    @Log(title = "修改完工入库", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult updateWipCompletion(@Validated @RequestBody WipCompletionVo wipCompletionVo) {
        return toAjax(wipCompletionService.updateWipCompletion(wipCompletionVo));
    }

    /**
     * 删除完工入库
     */
    @RequiresPermissions("warehouse:warehousing:finished:remove")
    @Log(title = "删除完工入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{idStr}")
    public AjaxResult deleteWipCompletion(@PathVariable("idStr") String idStr) {
        return toAjax(wipCompletionService.deleteWipCompletion(idStr));
    }

    /**
     * 导出
     */
    @RequiresPermissions("warehouse:warehousing:finished:export")
    @PostMapping("/wipCompletionMessageExport")
    public AjaxResult wipCompletionMessageExport(HttpServletResponse response, WipCompletionVo wipCompletionVo) throws IOException {
        List<WipCompletionExport> infoExport = wipCompletionService.getInfoExport(wipCompletionVo);
        ExcelUtil<WipCompletionExport> excelUtil = new ExcelUtil<WipCompletionExport>(WipCompletionExport.class);
        excelUtil.excelWriter(response, infoExport, "完工入库信息.xlsx");
        return AjaxResult.success();
    }


    /**
     * 完工入库（pda）生成方式为自动生成
     */
    @RequiresPermissions("warehouse:warehousing:finished:warehousing")
    @PostMapping("/wipCompletionPDA")
    @Log(title = "完工入库（pda）生成方式为自动生成", businessType = BusinessType.UPDATE)
    public AjaxResult autWipCompletionPda(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return AjaxResult.success(wipCompletionService.wipCompletionPda(pickingRestDbVo));
    }

    /**
     * 完工入库（pda）生成方式为手动生成
     */
    @RequiresPermissions("warehouse:warehousing:finished:warehousing")
    @PostMapping("/manualWipCompletionPDA")
    @Log(title = "完工入库（pda）生成方式为手动生成", businessType = BusinessType.UPDATE)
    public AjaxResult manualWipCompletionPda(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return AjaxResult.success(wipCompletionService.manualWipCompletionPda(pickingRestDbVo));
    }

    /**
     *完工入库拣货撤销
     */
    @PostMapping("/pickingCancellation")
    @Log(title = "完工入库拣货撤销", businessType = BusinessType.UPDATE)
    public AjaxResult pickingCancellation(@RequestBody PickingRestDbVo pickingRestDbVo) {
        return toAjax(wipCompletionService.pickingCancellation(pickingRestDbVo));
    }

    /**
     * 完工入库拣货完成
     */
    @PostMapping("/pickingCompleted")
    @Log(title = "完工入库拣货完成", businessType = BusinessType.UPDATE)
    public AjaxResult pickingCompleted(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return toAjax(wipCompletionService.pickingCompleted(pickingRestDbVo));
    }

    /**
     * 完工入库品质回传状态(fegin调用)
     */
    @PostMapping("/updatewipCompletionByQuality")
    public AjaxResult updatewipCompletionByQuality(@RequestBody UpdateStatusByQualityVo updateStatusByQualityVo) {
        return toAjax(wipCompletionService.updateStatusByQuality(updateStatusByQualityVo));
    }

    /**
     * 完工入库品质回传状态(品质处理fegin调用)
     */
    @PostMapping("/wipCompletionQualityStatus")
    public AjaxResult wipCompletionQualityStatus(@RequestBody ReturnStatusVo returnStatusVo) {
        return toAjax(wipCompletionService.wipCompletionQualityStatus(returnStatusVo));
    }

}
